<?php
/**
 *  Entrance File of the Control class.
 *
 * @Author:    slash<upsetcn@gmail.com>
 * @Package:   Interface of the View class. 
 * @Version:   $Id: auto_info.class.php 2010-12-31 19:58:27 $
 * @Show Note: 车辆牌照信息管理
 */
Class auto_info extends TheAdmin
{
		/* DataBase object */
		public $db;
	
		/* View the data page limit items */
		public $pageSize = 10;
		
		/**
		 *	Construct this module class.
		*/
		public function auto_info()
		{
		}
		public function set()
		{
			$this->TheAdmin();
			$this->admin_user_id = $this->userRequest->get('admin_user_id');
			//取得数据库连接
			$this->db = Factory::getDB("auto_info");
			//取得用户对于该类的权限
			$user_p = $this->get_user_popedom_by_class_name(__CLASS__);
			$class_p_arr = $this->show_p();
			//取得该类的权值数组
			$class_method = $class_p_arr['class_method'];
			//取得用户操作菜单
			$menu = array();
			foreach ($class_method as $k=>$v)
			{
				if( (parse_power($user_p,$v[2]) && (isset($v[3]) ?$v[3] :false)) || IS_ROOT)
				{
					array_push($menu,array('url'=>"?c=".__CLASS__."&m={$v[0]}",'title'=>$v[1]));
				}
			}
			$group_state_arr = array(1=>'正常',0=>'异常');
			$this->smarty->assign('group_state_arr',$group_state_arr);
			//用户有权菜单生成 可以添加其他操作
			$this->smarty->assign('menus',$menu);
			$this->smarty->assign("main_menus",$this->get_user_menus());
		}
	
		/**
		 *	Call class construct method.
		*/
		public function __construct()
		{
				$this->auto_info();
		}
	
		/**
		 *	default method.
		*/
		public function show()
		{
				global $select_array;
				$col = "auto_id";
				$where = 1;
				$select = isset($_GET['select']) ? $_GET['select'] : '';
				if($select){
					$where.=" and {$select} like '%{$_GET['select_time']}%'";
				}
				$auto_guest_name = isset($_GET['auto_guest_name']) ? trim($_GET['auto_guest_name']) : '';
				if($auto_guest_name){
					$where.=" and auto_brand like '%{$auto_guest_name}%'";
				}
				$rowNo = $this->db->getSumNumber($this->db->tableName,$where);
				//数据库执行sql语句出错
				//取得错误的sql语句
				//$error_sql = end($this->db->getDebugMsg());
				//echo $error_sql;
				if( 0 > $rowNo) goToIndex($rowNo);
				//分页
				$page = Factory::getBaseClass('pagination');
				$page->set($rowNo,$this->pageSize);
				$numrows = $this->pageSize;
				$offset  = $page->getSqlMove();
				$arr = $this->db->getAll($where,$col,$numrows,$offset);
				admin_log($this->admin_user_id,__METHOD__,'查看',1);
				//数据库执行select语句出错
				//取得错误的sql语句
				//$error_sql = end($this->db->getDebugMsg());
				//echo $error_sql;
				if(0 > $arr ) goToIndex($arr);
				$this->smarty->assign('select_array',$select_array);
				$this->smarty->assign('arr',$arr);
				//默认方法
				$tplFile = "auto_info.tpl";
				$this->smarty->assign("page_footer",$page->getFooter().$page->getJumpPage());
				$this->smarty->assign('act','list');

				$this->smarty->display($tplFile);
		}
		

				/**
		 *	到期车辆提醒
		*/
		public function tixing()
		{
				global $select_array;
				$col = "auto_id";
				$time[] = date('m-d',time()+3600*24*10);
				$time[] = date('m-d',time()+3600*24*3);
				$time[] = date('m-d',time()+3600*24);
			
				$str_arr[] = '十天后到期的车牌有';
				$str_arr[] = '三天后到期的车牌有';
				$str_arr[] = '明天后到期的车牌有';
				foreach($time as $k=>$v){
					$where = "auto_year_time like '%{$v}' or auto_year_pay_time like '%{$v}' or auto_two_insurance like '%{$v}' or auto_two_insurance2 like '%{$v}' or auto_two_insurance3 like '%{$v}' or auto_insurance like '%{$v}' or auto_server_time like '%{$v}' or auto_contract like '%{$v}' or jiaoqiangxian like '%{$v}' or shangyexian like '%{$v}'";
					$db = Factory::getDB("auto_info");
					
					$array[] = $db->getAll($where);
					//print_r($db->getDebug());
				}
				//时间标红
				$this->smarty->assign('red_time',$time);
				$this->smarty->assign('select_array',$select_array);
				$this->smarty->assign('arr',$array);
				$tplFile = "auto_info.tpl";
				$this->smarty->assign('act','tixing');
				$this->smarty->display($tplFile);
				/*
				$this->smarty->assign('select_array',$select_array);
				$this->smarty->assign('arr',$arr);
				//默认方法
				$tplFile = "auto_info.tpl";
				$this->smarty->assign("page_footer",$page->getFooter().$page->getJumpPage());
				$this->smarty->assign('act','list');

				$this->smarty->display($tplFile);*/
		}


		public function chazhao()
		{;
				global $select_array;
				$col = "auto_id";
				$cha_day = isset($_GET['cha_day']) ? intval($_GET['cha_day']) : 0;
				for($i=1;$i<=$cha_day;$i++){
					$time[] = date('m-d',time()+3600*24*$i);

				}
				//print_r($time);
				$str_arr[] = $cha_day.'天后到期的车牌有';
				foreach($time as $k=>$v){
					$where = "auto_year_time like '%{$v}' or auto_year_pay_time like '%{$v}' or auto_two_insurance like '%{$v}' or auto_server_time like '%{$v}' or auto_contract like '%{$v}' or jiaoqiangxian like '%{$v}' or shangyexian like '%{$v}'";
					$db = Factory::getDB("auto_info");
					
					$array[] = $db->getAll($where);
					//$error_sql = end($db->getDebugMsg());
					//echo $error_sql;
				}
				//unset($array[0]);
				foreach($array as $v){
					foreach($v as $nv){
						if(is_array($nv)){
							$newarray[] = $nv;
						}
					}
				}
				if(is_array($newarray)){
					foreach($newarray as $k=>$v){
						for($i=$k+1;$i<count($newarray);$i++){
							if($v['auto_id']==$newarray[$i]['auto_id']){
								//unset($newarray[$i]);
								$karr[] = $i;
							}	
						}
					}
				}
				if($karr){
					foreach($karr as $v){
						unset($newarray[$v]);
					}
				}
				//print_r($newarray);
				//时间标红
				$this->smarty->assign('red_time',$time);
				$this->smarty->assign('select_array',$select_array);
				$this->smarty->assign('arr',$newarray);
				$tplFile = "auto_info.tpl";
				$this->smarty->assign('act','chazhao');
				$this->smarty->assign('cha_day',$cha_day);
				$this->smarty->display($tplFile);
				/*
				$this->smarty->assign('select_array',$select_array);
				$this->smarty->assign('arr',$arr);
				//默认方法
				$tplFile = "auto_info.tpl";
				$this->smarty->assign("page_footer",$page->getFooter().$page->getJumpPage());
				$this->smarty->assign('act','list');

				$this->smarty->display($tplFile);*/
		}
		

		/**
		 *	add method.
		*/
		public function add()
		{
				//添加数据
				if(!isset($_REQUEST['post']) || trim($_REQUEST['post'])!=1)
				{
						//使用模版，展示提交界面
						$tplFile = "auto_info.tpl";
						$this->smarty->assign('act_title','添加');
						$this->smarty->assign('act','add');
						$this->smarty->display($tplFile);
				} else {
						//用户提交，把数据插入数据库
						$temp = $this->getValue();
						$insrt = $this->db->insert_auto_info($temp);
						$error_no = 0 > $insrt ?  $insrt :'-1501';
						admin_log($this->admin_user_id,__METHOD__,"id={$insrt}",2);
						//取得错误的sql语句
						/*$error_sql = end($this->db->getDebugMsg());
						echo $error_sql;exit;*/
						alertMsg($error_no, 'index.php?c=auto_info');
				}
		}
		
		/**
		 *	edit method.
		*/
		public function edit()
		{
				//取得id，也就是唯一索引
				$auto_id = isset($_REQUEST['auto_id']) ? intval(trim($_REQUEST['auto_id'])) : 0 ;
				//取得是否查看详情
				$is_show = isset($_REQUEST['is_show']) ? intval(trim($_REQUEST['is_show'])) : 0 ;
				if(!isset($_REQUEST['post']) || trim($_REQUEST['post'])!=1)
				{
						//使用模版，展示提交界面
						$result = $this->db->getOne($auto_id);
						if(empty($result))
						{
							alertMsg('-2001', 'index.php?c=auto_info');
						}
						foreach ($result AS $k=>$v)
						{
							$this->smarty->assign("{$k}",$v);
						}
						$tplFile = "auto_info.tpl";
						$this->smarty->assign('is_show',$is_show);
						$this->smarty->assign('act_title','修改');
						$this->smarty->assign('act','edit');
						$this->smarty->display($tplFile);
				} else {
						//用户提交，把数据插入数据库
						//设置where条件
						$where = "auto_id=$auto_id";
						$temp = $this->getValue(1);
						$update = $this->db->update_auto_info($temp,$where);
						admin_log($this->admin_user_id,__METHOD__,$where,2);
						$error_no = 0 > $update ?  $update :'-1502';
						//取得错误的sql语句
						//$error_sql = end($this->db->getDebugMsg());
						//echo $error_sql;
						alertMsg($error_no, 'index.php?c=auto_info');
				}
		}
		
		
		
		public function put_notice()
		{
			$id = isset($_GET['id']) ? intval($_GET['id']) : 0;
			$arr = $this->db->getOne($id);
			$array['auto_info'] = $arr['auto_info']."\n\r".'已通知--'.date('Y-m-d');
			$this->db->update_auto_info($array,"auto_id={$id}");
			alertMsg('-3002');
		}
		/**
		 *	del method.
		*/
		public function del()
		{
				//取得id，也就是唯一索引
				$auto_id = isset($_REQUEST['auto_id']) ? intval(trim($_REQUEST['auto_id'])) : 0 ;
				$where = "auto_id=$auto_id";
				$del=$this->db->delete_auto_info($where);
				admin_log($this->admin_user_id,__METHOD__,$where,3);
				$error_no = 0 > $del ?  $del :'-1503';
				//取得错误的sql语句
				//$error_sql = end($this->db->getDebugMsg());
				//echo $error_sql;
				alertMsg($error_no, 'index.php?c=auto_info');
		}
		
		/**
		 *	get request method.
		*/
		public function &getValue($isEdit=0)
		{
			$temp = array();
			$check = Factory::getBaseClass("checkBase");
			if($isEdit){
				$temp['auto_id'] = $_POST['auto_id'];
			} else {
				$temp['auto_id'] = "";
				$temp['auto_time'] = time();
				$temp['auto_admin_id'] = $_POST['auto_admin_id'];
			}
			//补充其他的字段
						$temp['auto_brand'] = $_POST['auto_brand'];
						$temp['auto_guest_name'] = $_POST['auto_guest_name'];
						$temp['auto_address'] = $_POST['auto_address'];
						$temp['auto_zip'] = $_POST['auto_zip'];
						$temp['auto_tel1'] = $_POST['auto_tel1'];
						$temp['auto_tel2'] = $_POST['auto_tel2'];
						$temp['auto_year_time'] = $_POST['auto_year_time'];
						$temp['auto_year_pay_time'] = $_POST['auto_year_pay_time'];
						$temp['auto_two_insurance'] = $_POST['auto_two_insurance'];
						$temp['auto_two_insurance2'] = $_POST['auto_two_insurance2'];
						$temp['auto_two_insurance3'] = $_POST['auto_two_insurance3'];
						$temp['auto_insurance'] = $_POST['auto_insurance'];
						$temp['auto_server_time'] = $_POST['auto_server_time'];
						$temp['auto_server_user'] = $_POST['auto_server_user'];
						$temp['auto_contract'] = $_POST['auto_contract'];
						$temp['auto_info'] = $_POST['auto_info'];
						$temp['yingyun_no'] = $_POST['yingyun_no'];
						$temp['yingyun_time'] = $_POST['yingyun_time'];
						$temp['bianhao'] = $_POST['bianhao'];
						$temp['jiaoqiangxian'] = $_POST['jiaoqiangxian'];
						$temp['shangyexian'] = $_POST['shangyexian'];
						$temp['fuwufiejine'] = $_POST['fuwufiejine'];
						$temp['pinpaixinghao'] = $_POST['pinpaixinghao'];
						$temp['dengjiriqi'] = $_POST['dengjiriqi'];
						$temp['chejia'] = $_POST['chejia'];
						$temp['dunwei'] = $_POST['dunwei'];
						
						
						admin_log($this->admin_user_id,__METHOD__,serialize($temp),2);	
			return $temp;
		}
		/**
		 *	get class method and popedom.
		*/
		public function show_p()
		{
			//该方法为，提供用户使用方法定义，非web用户权限相关不用写
			return array(
				/* 定义该类中文名称*/
				'class_name'=>array(__CLASS__,'车辆牌照管理'),
				'class_method' =>array(
					//array（方法名，中文解释，权值,是否为菜单）
					'show'  => array('show','列表',1,true),
					'add'  => array('add','添加',2,true),
					'tixing'  => array('tixing','到期车辆提醒',16,true),
					'edit'  => array('edit','编辑',4,false),
					'del'  => array('del','删除',8,false),
					'chazhao' => array('chazhao','查找',32,false),
					'put_notice' => array('put_notice','通知',64,false),
					'show_p'=> array('show_p','',0,false)
					
				)
			);
		}
}

?>